/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.model.manteniment.auxiliars;

import comandaXM.classes.manteniment.auxiliar.*;
import comandaXM.excepcions.ExceptionHibernate;
import comandaXM.utils.HibernateUtil;
import comandaXM.utils.Utils;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;

/**
 *
 * @author xmarti
 */
//TODO: CAL IMPLEMENTAR ELS TIPUS D¡'IVA, I REVISAR LA CERCA DE PROVINCIES
public class AuxiliarModel extends HibernateUtil {
     public List<Pais> getListPaisos() throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Pais> resultList = new ArrayList<Pais>();
        try
        {            
            resultList= session.createQuery("FROM Pais c order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    
     public List<Pais> getListPaisos(String textToSearch) throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int codi=0; 
        List<Pais> resultList = new ArrayList<Pais>();
        try
        {            
            if(Utils.isInteger(textToSearch)){
                codi=Integer.parseInt(textToSearch); 
            }            
            resultList= session.createQuery("FROM Pais c Where c.codi="+ codi + " OR c.nom LIKE '"+ textToSearch  +"%' order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            System.out.println (he) ; 
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
            
        }
        return resultList;
    }
     
    public List<Provincia> getListProvincies() throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Provincia> resultList = new ArrayList<Provincia>();
        try
        {            
            
            resultList= session.createQuery("FROM Provincia c order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    public List<Provincia> getListProvincies(String textToSearch) throws ExceptionHibernate{
        
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<Provincia> resultList = new ArrayList<Provincia>();
        try
        {            
        
            resultList= session.createQuery("FROM Provincia c Where c.codi LIKE '"+ textToSearch + "%' OR c.nom LIKE '"+ textToSearch  +"%' order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    public List<TipusIva> getListTipusIvaActive() throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TipusIva> resultList = new ArrayList<TipusIva>();
        try
        {                        
            resultList= session.createQuery("FROM TipusIva c Where c.actiu=true order by c.impost" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    public List<TipusIva> getListTipusIva(String textToSearch) throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TipusIva> resultList = new ArrayList<TipusIva>();
        try
        {                        
            resultList= session.createQuery("FROM TipusIva c Where c.codi LIKE '%"+ textToSearch + "%' OR c.nom LIKE '"+ textToSearch  +"%' order by c.impost" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    public List<TipusPagament> getListTipusPagamentActive() throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TipusPagament> resultList = new ArrayList<TipusPagament>();
        try
        {                        
            resultList= session.createQuery("FROM TipusPagament c Where c.actiu=true order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
    public List<TipusPagament> getListTipusPagament(String textToSearch) throws ExceptionHibernate{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<TipusPagament> resultList = new ArrayList<TipusPagament>();
        try
        {                        
            resultList= session.createQuery("FROM TipusPagament c Where c.codi LIKE '%"+ textToSearch + "%' OR c.nom LIKE '%"+ textToSearch  +"%' order by c.codi" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernate(ExceptionHibernate.ERR_SELECT);
        }
        return resultList;
    }
     public int add(Pais object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                        
                session.save(object);                                                
                state= object.getId();
                session.getTransaction().commit();

        }catch (Exception e){			
                session.getTransaction().rollback();		
                if (e.getCause().toString().contains("name_UNIQUE") ){
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT_NAME_UNIQUE);
                }else{
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT);			
                }  
                
                
        }		
        return state;
	}
     public int add(Provincia object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                        
                session.save(object);                                                
                state= object.getId();
                session.getTransaction().commit();

        }catch (Exception e){			
                session.getTransaction().rollback();		
                if (e.getCause().toString().contains("name_UNIQUE") ){
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT_NAME_UNIQUE);
                }else{
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT);			
                }  
                
                
        }		
        return state;
	}
     public int add(TipusIva object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                        
                session.save(object);                                                
                state= object.getId();
                session.getTransaction().commit();

        }catch (Exception e){			
                session.getTransaction().rollback();	
                System.out.println (e.getMessage());
                if (e.getCause().toString().contains("name_UNIQUE") ){
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT_NAME_UNIQUE);
                }else{
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT);			
                }                  
        }		
        return state;
	}
     public int add(TipusPagament object) throws ExceptionHibernate {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int state=0;                
        try{			                        
                session.save(object);                                                
                state= object.getId();
                session.getTransaction().commit();

        }catch (Exception e){			
                session.getTransaction().rollback();	
                System.out.println (e.getMessage());
                if (e.getCause().toString().contains("name_UNIQUE") ){
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT_NAME_UNIQUE);
                }else{
                    throw new ExceptionHibernate(ExceptionHibernate.ERR_INSERT);			
                }                  
        }		
        return state;
	}
}
